package edu.lingnan.lmlibrary.service.impl;

import edu.lingnan.lmlibrary.common.JSONResult;
import edu.lingnan.lmlibrary.service.StudentService;

import edu.lingnan.lmlibrary.util.HttpRequestRouteStudentInfo;
import net.minidev.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@Service
public class StudentServiceImpl implements StudentService {

    @Override
    public JSONResult registerStudent(String no, String name, String password, RestTemplate restTemplate) {
        HashMap<String, String> map = new HashMap<>();
        if(no ==null || no.trim().equals(""))
        {
            return new JSONResult(201,"学号不能为空",null);
        }
        if(name == null || name.trim().equals(""))
        {
            return new JSONResult(201,"姓名不能为空",null);
        }
        if(password == null || password.trim().equals(""))
        {
            return new JSONResult(201,"密码不能为空",null);
        }
        map.put("no",no);
        map.put("name",name);
        map.put("password",password);
        ResponseEntity<JSONObject> post = HttpRequestRouteStudentInfo.httpRequestRouteStudentInfo("http://120.77.247.146:1281/student",
                "post", map, restTemplate);
        JSONObject body = post.getBody();
        System.out.println(body);
        return new JSONResult((Integer) body.get("code"),body.getAsString("msg"),null);
    }
}
